import axios from '@ohos/axios'
import { promptAction } from '@kit.ArkUI'
import type AnyObject from '../../model/HttpModel'

const request = axios.create({
  baseURL: 'http://192.168.0.21:6060'
})

// 请求拦截器
request.interceptors.request.use(
  (config) => {
    // 将来会添加token参数
    return config
  }
)

//响应拦截器
request.interceptors.response.use(
  (res) => {
    if (res.data.code === 200) {
      return res.data.data
    } else {
      //   错误提示
      promptAction.showToast(res.data.message)
      return Promise.reject(res.data.message)
    }
  },
  (err) => {
    promptAction.showToast(err.message)
    return Promise.reject(err.message)
  }
)

//请求方法
export default class Http {
  get<T>(url: string, params?: AnyObject) {
    return request.get<any, T>(url, {
      params
    })
  }

  post<T>(url: string, data?: AnyObject) {
    return request.post<any, T>(url, data)
  }

  put<T>(url: string, data?: AnyObject) {
    return request.put<any, T>(url, data)
  }

  delete<T>(url: string, params?: AnyObject) {
    return request.delete<any, T>(url, {
      params
    })
  }
}